package com.offen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offen.entity.Student;
import com.offen.entity.StudentCourse;
import com.offen.mapper.StudentCourseMapper;
import com.offen.mapper.StudentMapper;
import com.offen.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishucai
 * @since 2022-06-17
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Resource
    private StudentCourseMapper studentCoursemapper;

    @Override
    public QueryWrapper<Student> update1(Long id) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("id", id);
        return studentQueryWrapper;
    }


    /**
     * 分页查询学生信息
     *
     * @param student
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @Override
    public Page<Student> selectStudent(Student student, Integer pageSize, Integer pageNumber) {
        // 分页条件
        Page page = new Page(pageSize, pageNumber);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(student.getStudName())) {
            queryWrapper.like("stud_name", student.getStudName());
        }
        if (!StringUtils.isEmpty(student.getSex())) {
            queryWrapper.eq("sex", student.getSex());
        }
        // 年级
        if (!StringUtils.isEmpty(student.getGrade())) {
            queryWrapper.eq("grade", student.getGrade());
        }

        Page page1 = page(page, queryWrapper);

        return page1;
    }

    // 删除学生时，删除对应的课程关系
    @Override
    public boolean deleteStudent(Student student) {
        // 根据id删除学生信息
        boolean b = this.removeById(student);
        if (b) {
            // 删除对应关系
            Long id = student.getId();

            QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<StudentCourse>();
            queryWrapper.eq("sid", id);
            int delete = studentCoursemapper.delete(queryWrapper);
            return delete > 0;
        }
        return false;
    }

    //
}
